package com.hafu.view.kehu;

import com.hafu.model.KhInfo;
import com.hafu.service.KhInfoService;
import com.hafu.util.StringUtil;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;
import javax.swing.LayoutStyle;


public class ManageKeHuFrm extends JFrame {
    private KhInfoService khInfoService = new KhInfoService();
    private int chooseId;


    public ManageKeHuFrm() {
        this.initComponents();
        this.fillTable();
    }

    private void fillTable() {
        DefaultTableModel dtm = (DefaultTableModel)this.table1.getModel();
        dtm.setRowCount(0);
        ArrayList<KhInfo> khInfoList = this.khInfoService.getKhInfoByName(new KhInfo());
        Iterator var3 = khInfoList.iterator();

        while(var3.hasNext()) {
            KhInfo khInfo = (KhInfo)var3.next();
            Object[] rowData = new Object[]{khInfo.getId(), khInfo.getName(), khInfo.getTel(), khInfo.getArea()};
            dtm.addRow(rowData);
        }

    }

    private void button1(ActionEvent e) {
        this.textField2.setText("");
        this.textField3.setText("");
        DefaultTableModel dtm = (DefaultTableModel)this.table1.getModel();
        dtm.setRowCount(0);
        String name = this.textField1.getText();
        KhInfo khInfo = new KhInfo();
        khInfo.setName(name);
        ArrayList<KhInfo> khInfoByName = this.khInfoService.getKhInfoByName(khInfo);
        if (khInfoByName.size() > 0) {
            Iterator var6 = khInfoByName.iterator();

            while(var6.hasNext()) {
                KhInfo khInfo1 = (KhInfo)var6.next();
                Object[] rowData = new Object[]{khInfo1.getId(), khInfo1.getName(), khInfo1.getTel()};
                dtm.addRow(rowData);
            }
        } else {
            JOptionPane.showMessageDialog((Component)null, "没有找到学生信息");
        }

    }

    private void table1MouseClicked(MouseEvent e) {
        int row = this.table1.getSelectedRow();
        DefaultTableModel dtm = (DefaultTableModel)this.table1.getModel();
        this.chooseId = (Integer)dtm.getValueAt(row, 0);
        String name = (String)dtm.getValueAt(row, 1);
        String tel = (String)dtm.getValueAt(row, 2);
        String area = (String)dtm.getValueAt(row, 3);
        this.textField2.setText(name);
        this.textField3.setText(tel);
        this.textField4.setText(area);
    }

    private void button2(ActionEvent e) {
        int row = this.table1.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog((Component)null, "请选择要修改的学生信息");
        } else {
            String name = this.textField2.getText();
            String tel = this.textField3.getText();
            String area = this.textField4.getText();
            DefaultTableModel dtm = (DefaultTableModel)this.table1.getModel();
            int id = (Integer)dtm.getValueAt(row, 0);
            if (StringUtil.isEmpty(name)) {
                JOptionPane.showMessageDialog((Component)null, "学生名称不能为空");
            }

            if (StringUtil.isEmpty(tel)) {
                JOptionPane.showMessageDialog((Component)null, "学生电话不能为空");
            }
            if (StringUtil.isEmpty(tel)) {
                JOptionPane.showMessageDialog((Component)null, "学生地址不能为空");
            }
            KhInfo khInfo = new KhInfo(id, name, tel, area);
            if (!StringUtil.isPhoneNumber(tel)) {
                JOptionPane.showMessageDialog((Component)null, "学生电话号码格式不正确");
            } else {
                ArrayList<KhInfo> khInfoByName = this.khInfoService.getKhInfoByName(khInfo);
                if (khInfoByName.size() > 1) {
                    JOptionPane.showMessageDialog((Component)null, "名字已存在,请勿重复");
                } else if (khInfoByName.size() == 1 && ((KhInfo)khInfoByName.get(0)).getId() != this.chooseId) {
                    JOptionPane.showMessageDialog((Component)null, "名字已存在,请勿重复");
                } else {
                    Boolean updateKhInfo = this.khInfoService.updateKhInfo(khInfo);
                    if (updateKhInfo) {
                        JOptionPane.showMessageDialog((Component)null, "修改成功");
                        this.dispose();
                    } else {
                        JOptionPane.showMessageDialog((Component)null, "修改失败");
                    }

                }
            }
        }
    }

    private void button3(ActionEvent e) {
        int row = this.table1.getSelectedRow();
        if (row == -1) {
            JOptionPane.showMessageDialog((Component)null, "请选择要删除的客户信息");
        } else {
            DefaultTableModel dtm = (DefaultTableModel)this.table1.getModel();
            int id = (Integer)dtm.getValueAt(row, 0);
            Boolean deleteKhInfo = this.khInfoService.deleteKhInfo(id);
            if (deleteKhInfo) {
                JOptionPane.showMessageDialog((Component)null, "删除成功");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog((Component)null, "删除失败");
            }

        }
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - aaaa
        label1 = new JLabel();
        textField1 = new JTextField();
        button1 = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        label2 = new JLabel();
        label3 = new JLabel();
        textField2 = new JTextField();
        textField3 = new JTextField();
        button2 = new JButton();
        button3 = new JButton();
        label4 = new JLabel();
        textField4 = new JTextField();

        //======== this ========
        setTitle("\u7ba1\u7406\u5b66\u751f\u4fe1\u606f");
        var contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u5b66\u751f\u540d\u5b57");

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addActionListener(e -> button1(e));

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setModel(new DefaultTableModel(
                new Object[][] {
                    {null, null, null, ""},
                    {null, null, null, null},
                },
                new String[] {
                    "\u7f16\u53f7", "\u540d\u5b57", "\u7535\u8bdd", "\u5730\u5740"
                }
            ));
            table1.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    table1MouseClicked(e);
                    table1MouseClicked(e);
                }
            });
            scrollPane1.setViewportView(table1);
        }

        //---- label2 ----
        label2.setText("\u5b66\u751f\u540d\u5b57");

        //---- label3 ----
        label3.setText("\u5b66\u751f\u7535\u8bdd");

        //---- button2 ----
        button2.setText("\u4fee\u6539");
        button2.addActionListener(e -> button2(e));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addActionListener(e -> button3(e));

        //---- label4 ----
        label4.setText("\u5b66\u751f\u5730\u5740");

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(41, 41, 41)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 611, GroupLayout.PREFERRED_SIZE)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 93, GroupLayout.PREFERRED_SIZE)
                                    .addGap(35, 35, 35)
                                    .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 174, GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(button1, GroupLayout.PREFERRED_SIZE, 195, GroupLayout.PREFERRED_SIZE))))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(54, 54, 54)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2)
                                .addComponent(label3)
                                .addComponent(label4))
                            .addGap(56, 56, 56)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE)
                                .addComponent(textField4, GroupLayout.PREFERRED_SIZE, 119, GroupLayout.PREFERRED_SIZE))
                            .addGap(319, 319, 319)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(button2)
                                .addComponent(button3))))
                    .addContainerGap(189, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(35, 35, 35)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(button1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addComponent(label1))
                    .addGap(28, 28, 28)
                    .addComponent(scrollPane1, GroupLayout.PREFERRED_SIZE, 262, GroupLayout.PREFERRED_SIZE)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(34, 34, 34)
                            .addComponent(button2)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                            .addComponent(button3)
                            .addGap(40, 40, 40))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label2)
                                .addComponent(textField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label3)
                                .addComponent(textField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addGap(18, 18, 18)
                            .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                .addComponent(label4)
                                .addComponent(textField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(13, Short.MAX_VALUE))))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - aaaa
    private JLabel label1;
    private JTextField textField1;
    private JButton button1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JLabel label2;
    private JLabel label3;
    private JTextField textField2;
    private JTextField textField3;
    private JButton button2;
    private JButton button3;
    private JLabel label4;
    private JTextField textField4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
